class Graph{
    constructor(isDirected = false){
        this.isDirected = isDirected; //无向和有向
        this.vertices = []; //存放顶点的数组
        this.edge = new Map(); //邻接表
    }

    //添加顶点
    addVertex(v){
        if(!this.vertices.includes(v)){
            this.vertices.push(v);
            this.edge.set(v,[]);
        }
    }

    //添加边
    addEdge(v,w){
        if(!this.vertices.includes(v)){
            this.addVertex(v)
        }

        if(!this.vertices.includes(w)){
            this.addVertex(w)
        }

        this.edge.get(v).push(w);
        if(!this.isDirected){
            this.edge.get(w).push(v);
        }
    }

    //获取顶点
    getVertices(){
        return this.vertices;
    }

    //
    getEgde(){
        return this.edge;
    }

    toString(){
        let str = '';
        for(let i = 0;i<this.vertices.length;i++){
            str += `${this.vertices[i]}--->`;
            let edgeList = this.edge.get(this.vertices[i]);
            for(let j = 0;j<edgeList.length;j++){
                str += `${edgeList[j]}`;
            }
            str += '\n';
        }

        return str
    }
}

let g = new Graph();

g.addVertex('A');

g.addVertex('B');

g.addEdge('C','D');



console.log(g.getVertices());

console.log(g.getEgde())

console.log(g.toString())